/*
 * Copyright 2010 J Patrick Davenport
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package v4view.web;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import java.io.IOException;

import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;

public class ATests extends A {
	@Test
	public void addNoHref() {
		this.setHref(null);
		assertNull(this.getHref());
	}

	@Test
	public void addNoQuery() {
		this.setHref("index.jsp");
		assertEquals("index.jsp", this.getHref());
	}

	@Test
	public void addWithOnlyQueryIndicator() {
		this.setHref("index.jsp?");
		assertEquals("index.jsp", this.getHref());
	}

	@Test
	public void addWithOnlyOneParam() {
		this.setHref("index.jsp?name=value");
		assertEquals("index.jsp?name=value", this.getHref());
	}

	@Test
	public void addWithOverwrite() {
		this.setHref("index.jsp?name=value");
		this.addParameter("name", "tim");
		assertEquals("index.jsp?name=tim", this.getHref());
	}

	@Test
	public void addNewParams() {
		this.addParameter("name", "_value");
		this.setHref("koe.jsp");
		assertEquals("koe.jsp?name=_value", this.getHref());
	}

	@Test
	public void addMixedParams() {
		this.setHref("koe.jsp?name=value");
		this.addParameter("name", "jimmy").addParameter("zip", "46060");
		assertEquals("koe.jsp?zip=46060&name=jimmy", this.getHref());
	}

	@Test
	public void removeParameter() {
		this.setHref("myhouse.jsp");
		this.addParameter("name", "_value");
		this.removeParameter("name");
		assertEquals("myhouse.jsp", this.getHref());
	}

	@Test
	public void testAppendContextPath() throws IOException {
		final MockHttpServletResponse response = new MockHttpServletResponse();
		final MockHttpServletRequest request = new MockHttpServletRequest();
		this.setHref("/bob");
		request.setContextPath("MyTest");
		this.setAppendAppContext(true);
		this.setRequest(request);
		this.setResponse(response);
		this.render(request, response);

		final String contentAsString = response.getContentAsString();
		assertEquals("<a href=\"MyTest/bob\"></a>", contentAsString);
	}
}
